home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Gfx / Edit / TSMorph / src / unpacker.c < prev    next >
C/C++ Source or Header  |  1994-10-30  |  2KB  |  65 lines

  1. #include "iffp/ilbm.h"
  2. #include "iffp/packer.h"
  3.  
  4. /*----------------------------------------------------------------------*
  5.  * unpacker.c Convert data from "cmpByteRun1" run compression. 11/15/85
  6.  *
  7.  * Based on code by Jerry Morrison and Steve Shaw, Electronic Arts.
  8.  * This software is in the public domain.
  9.  *
  10.  *    control bytes:
  11.  *     [0..127]   : followed by n+1 bytes of data.
  12.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  13.  *     -128       : NOOP.
  14.  *
  15.  * This version for the Commodore-Amiga computer.
  16.  *----------------------------------------------------------------------*/
  17.  
  18. /*----------- UnPackRow ------------------------------------------------*/
  19.  
  20. #define UGetByte()    (*source++)
  21. #define UPutByte(c)    (*dest++ = (c))
  22.  
  23. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  24.  * and destination pointers until it produces dstBytes bytes.
  25.  */
  26.  
  27. BOOL unpackrow(BYTE **pSource, BYTE **pDest, WORD srcBytes0, WORD dstBytes0)
  28.     {
  29.     register BYTE *source = *pSource;
  30.     register BYTE *dest   = *pDest;
  31.     register WORD n;
  32.     register BYTE c;
  33.     register WORD srcBytes = srcBytes0, dstBytes = dstBytes0;
  34.     BOOL error = TRUE;    /* assume error until we make it through the loop */
  35.     WORD minus128 = -128;  /* get the compiler to generate a CMP.W */
  36.  
  37.     while( dstBytes > 0 )  {
  38.     if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  39.         n = UGetByte();
  40.  
  41.         if (n >= 0) {
  42.         n += 1;
  43.         if ( (srcBytes -= n) < 0 )  goto ErrorExit;
  44.         if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  45.         do {  UPutByte(UGetByte());  } while (--n > 0);
  46.         }
  47.  
  48.         else if (n != minus128) {
  49.         n = -n + 1;
  50.         if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  51.         if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  52.         c = UGetByte();
  53.         do {  UPutByte(c);  } while (--n > 0);
  54.         }
  55.     }
  56.     error = FALSE;    /* success! */
  57.  
  58.   ErrorExit:
  59.     *pSource = source;  *pDest = dest;
  60.     return(error);
  61.     }
  62.  
  63.  
  64. /* end */
  65.